package com.lz.comm.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by you on 2017/7/7.
 * Material进度
 */

public class MaterialProgress extends View {

    private MaterialProgressDrawable mDrawable;

    private boolean isStart;

    public MaterialProgress(Context context) {
        super(context);
        init(context);
    }

    public MaterialProgress(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MaterialProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mDrawable = new MaterialProgressDrawable(context, this);
        mDrawable.setBackgroundColor(Color.WHITE);
        mDrawable.setCallback(this);
        mDrawable.showArrow(true);
        mDrawable.setAlpha(255);
        setVisibility(View.GONE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = mDrawable.getMinimumWidth();
        int height = mDrawable.getMinimumHeight();
        mDrawable.setBounds(0, 0, width, height);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mDrawable.draw(canvas);
    }

    @Override
    public void invalidateDrawable(Drawable dr) {
        if (dr == mDrawable) {
            invalidate();
        } else {
            super.invalidateDrawable(dr);
        }
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (visibility == View.GONE) {
            mDrawable.stop();
            isStart = false;
        } else if (!isStart) {
            isStart = true;
            mDrawable.start();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mDrawable.stop();
        isStart = false;
    }

    public void show() {
        setVisibility(View.VISIBLE);
    }

    public void hiden() {
        setVisibility(View.GONE);
    }

}
